﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;

namespace DongleTest.DB
{
    [DataContract]
    public class RegiestInfo
    {
        [DataMember(Order = 0)]
        public string sn { get; set; }

        [DataMember(Order = 1)]
        public string macid { get; set; }

        [DataMember(Order = 2)]
        public long timestamp { get; set; }

        //[DataMember(Order = 3)]
        //public string signature { get; set; }

    }

    [DataContract]
    public class ReciveInfo
    {
        [DataMember(Order = 0)]
        public int code { get; set; }

        [DataMember(Order = 1)]
        public string msg { get; set; }

        [DataMember(Order = 2)]
        public Data data { get; set; }
    }

    [DataContract]
    public class Data
    {
        [DataMember(Order = 0)]
        public string qrticket { get; set; }
    }

    public class Json
    {
        /// <summary>
        /// json字符串转换成json对象
        /// </summary>
        /// <param name="dataString"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string dataString)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            var mStream = new MemoryStream(Encoding.UTF8.GetBytes(dataString));
            T rec = (T)serializer.ReadObject(mStream);
            return rec;
        }

        /// <summary>
        /// json对象转换成json字符串
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string ObjectToJson<T>(T json)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            var stream = new MemoryStream();
            serializer.WriteObject(stream, json);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            string dataString = Encoding.UTF8.GetString(dataBytes);
            return dataString;

        }
    }
}
